﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using ConfigurationManagement.Data;
using Environment = ConfigurationManagement.Data.Environment;

namespace ConfigurationManagement.MsBuild.Tests.TaskTests
{
    class TestUtilities
    {
        public static void RemoveBasicTestData()
        {
            var ctx = new Data.ConfigurationManagementDataContext();
            ctx.SettingValues.DeleteAllOnSubmit(ctx.SettingValues);
            ctx.SolutionSettings.DeleteAllOnSubmit(ctx.SolutionSettings);
            ctx.Solutions.DeleteAllOnSubmit(ctx.Solutions);
            ctx.Environments.DeleteAllOnSubmit(ctx.Environments);
            ctx.Projects.DeleteAllOnSubmit(ctx.Projects);
            ctx.SubmitChanges();
        }
        public static void AddBasicTestData()
        {
            var ctx = new Data.ConfigurationManagementDataContext();
            ctx.Projects.InsertOnSubmit(CreateProject(1));
            ctx.Projects.InsertOnSubmit(CreateProject(2));
            ctx.Projects.InsertOnSubmit(CreateProject(3));
            ctx.SubmitChanges();
        }

        public static Project CreateProject(int index)
        {
            var project1 = new Data.Project
                               {
                                   ProjectDescription = "Project" + index,
                                   ProjectName = "Project" + index,
                                   Environments = new EntitySet<Environment>
                                                      {
                                                          new Environment
                                                              ()
                                                              {
                                                                  EnvironmentDEscription
                                                                      =
                                                                      "Environment1",
                                                                  EnvironmentName
                                                                      =
                                                                      "Environment1"
                                                              },
                                                          new Environment
                                                              ()
                                                              {
                                                                  EnvironmentDEscription
                                                                      =
                                                                      "Environment2",
                                                                  EnvironmentName
                                                                      =
                                                                      "Environment2"
                                                              }
                                                      }
                               };
            project1.Solutions = new EntitySet<Solution>() { CreateSolution(project1, 1), CreateSolution(project1, 2) };
            return project1;
        }
        public static Solution CreateSolution(Project p, int index)
        {
            var solution = new Solution()
                               {
                                   Project = p,
                                   SolutionDescription = "Solution" + index,
                                   SolutionName = "Solution" + index
                               };
            solution.SolutionSettings = new EntitySet<SolutionSetting>
                                            {
                                                CreateSolutionSetting(solution, 1),
                                                CreateSolutionSetting(solution, 2),
                                                CreateSolutionSetting(solution, 3)
                                            };
            return solution;
        }
        public static SolutionSetting CreateSolutionSetting(Solution s, int index)
        {
            var setting = new SolutionSetting() { DefaultValue = "Solution" + index, SettingDescription = "Solution" + index, SettingTag = "Solution" + index, Solution = s };

            foreach(var e in s.Project.Environments)
            {
                setting.SettingValues.Add(CreateSettingValue(setting, e, 1));                
            }
            return setting;
        }
        public static SettingValue CreateSettingValue(SolutionSetting s, Environment e, int index)
        {
            var setting = new SettingValue() { Environment = e, SettingValue1 = "settingvalue" + index, SolutionSetting = s };
            return setting;
        }


        public static void CleanAuditData()
        {
            var ctx = new Data.ConfigurationManagementDataContext();
            ctx.SolutionSettingChanges.DeleteAllOnSubmit(ctx.SolutionSettingChanges);
            ctx.SettingValuesChanges.DeleteAllOnSubmit(ctx.SettingValuesChanges);
            ctx.SubmitChanges();
        }
        public static void SetupAuditData()
        {
            var ctx = new Data.ConfigurationManagementDataContext();
            ctx.SolutionSettingChanges.InsertOnSubmit(new SolutionSettingChange()
            {
                ChangeAction = "Add",
                ChangedBy = "Me",
                ChangedDateTime = DateTime.Now,
                ChangeID = 1,
                ProjectID = 2,
                ProjectName = "ProjectName",
                SolutionID = 1,
                SolutionName = "SolutionName",
                SolutionSettingDefaultValue = "Default",
                SolutionSettingDescription = "Description",
                SolutionSettingID = 1,
                SolutionSettingTag = "Tag"
            });
            ctx.SolutionSettingChanges.InsertOnSubmit(new SolutionSettingChange()
            {
                ChangeAction = "Add",
                ChangedBy = "Me",
                ChangedDateTime = DateTime.Now,
                ChangeID = 1,
                ProjectID = 2,
                ProjectName = "ProjectName",
                SolutionID = 1,
                SolutionName = "SolutionName",
                SolutionSettingDefaultValue = "Default",
                SolutionSettingDescription = "Description",
                SolutionSettingID = 1,
                SolutionSettingTag = "Tag"
            });
            ctx.SettingValuesChanges.InsertOnSubmit(new SettingValuesChange()
            {
                ChangeAction = "Add",
                ChangedBy = "Me",
                ChangedDateTime = DateTime.Now,
                ChangeID = 1,
                ProjectID = 2,
                ProjectName = "ProjectName",
                SolutionID = 1,
                SolutionName = "SolutionName",
                EnvironmentID = 1,
                EnvironmentName = "EnvironmentName",
                SettingValue = "Settingvalue",
                SolutionSettingID = 1,
                SolutionSettingTag = "Tag"
            });
            ctx.SettingValuesChanges.InsertOnSubmit(new SettingValuesChange()
            {
                ChangeAction = "Add",
                ChangedBy = "Me",
                ChangedDateTime = DateTime.Now,
                ChangeID = 1,
                ProjectID = 2,
                ProjectName = "ProjectName",
                SolutionID = 1,
                SolutionName = "SolutionName",
                EnvironmentID = 1,
                EnvironmentName = "EnvironmentName",
                SettingValue = "Settingvalue",
                SolutionSettingID = 1,
                SolutionSettingTag = "Tag"
            });
            ctx.SubmitChanges();
        }
    }
}
